/**
 * Created by Administrator on 2018/2/1 0001.
 */

type = function (value, row, index) {
    var type = row.type;
    var s;
    if (type == 1) {
        s = '广告位';
    } else if (type == 2) {
        s = '推荐商家';
    } else if (type == 3) {
        s = '商品特价';
    } else {
        s = '红包广告位';
    }
    return [
        '<div>' + s + '</div>'
    ].join('');
};

months = function (value, row, index) {
    var months = row.months;
    var type = row.type;
    var s = "";
    if (type == 1 || type == 4) {
        s = months + '月';
    } else if (type == 2) {
        s = months + '月';
    } else if (type == 3) {
        s = months + '天';
    }
    return [
        '<div>' + s + '</div>'
    ].join('');
};

$("#addTime").click(function () {
    showModel("addSelectTime-div", function () {
        $("#time").val('');
        $("#times").val('');
        $("#price").val('');
        $("#type").val(0);
    })
});

/**
 * 添加
 */
bindClick("button[data-event=submit-addSelectTime]", function () {

    var time = $("#time").val();
    var times = $("#times").val();
    if (time != "") {
        if (time == "") {
            hint("请填写时间");
            return;
        }
    } else {
        if (times == "") {
            hint("请填写时间");
            return;
        }
    }

    var price = $("#price").val();
    if (price == "") {
        hint("请填写价格");
        return;
    }

    var type = $("#type").val();
    if (type == "") {
        hint("请选择类型");
        return;
    }
    $("#types").val(type);

    var form = $("#addTimeFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/time/addSelectTime'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('添加成功');
            //关闭model方法
            closeModel('addSelectTime-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});


Formatter = function () {
    return [
        '<div><a class="update" href="javascript:void(0)">修改</a>' +
        '&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" style="color: red">删除</a></div>'
    ].join('');
};

window.Event = {
    'click .update': function (e, value, row) {
        showModel("updateSelectTime-div", function () {
            $("#id").val(row.id);
            $("#oldMoney").val(row.oldMoney);
            $("#updateRemark").val(row.remark);
            $("#months").val(row.months);
            $("#money").val(row.money);
            $("#typeEdit").val(row.type);
        })
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/time/delete'), {
                id: row.id
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};


/**
 * 修改
 */
bindClick("button[data-event=submit-updateSelectTime]", function () {
    var time = $("#months").val();
    if (time == "") {
        hint("请填写时间");
        return;
    }

    var price = $("#money").val();
    if (price == "") {
        hint("请填写价格");
        return;
    }

    var type = $("#typeEdit").val();
    if (type == 0) {
        hint("请选择类型");
        return;
    }
    $("#updateType").val(type);

    var form = $("#updateTimeFrom");
    var param = form.formToArray();
    $.post($().AbsUrlParse('/web/time/updateSelectTime'), param, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功');
            //关闭model方法
            closeModel('updateSelectTime-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

var showTime = function () {
    var type = $("#type").val();
    if (type == 1 || type == 4) {
        $("#show1").show();
        $("#show2").hide();
    } else if (type == 2) {
        $("#show1").show();
        $("#show2").hide();
    } else if (type == 3) {
        $("#show1").hide();
        $("#show2").show();
    }
};

bindClick("button[data-event=submit-close]", function () {
    $("#show1").hide();
    $("#show2").hide();
});